package com.hk.service.yingmi.impl;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hk.commons.utils.yingmi.YingmiApiUtils;
import com.hk.commons.vo.TAVo;
import com.hk.service.yingmi.IYingmiTestConfirm;

@Service
public class YingmiTestConfirmImpl implements IYingmiTestConfirm{

	@Autowired
	private YingmiApiUtils utils;
	
	@Override
	public String createTAOrder(TAVo av) {
		String path = "/backdoor/createTAOrder";
		
		Map<String , String> params = new HashMap<>();
		
		params = this.objectToMap(av);
		String result = utils.post(path, params);
		
		return result;
		
	}

	public Map<String, String> objectToMap(Object obj) {
		if(obj == null){
			return null;
		}
		Map<String, String> map = new HashMap<>();
		BeanInfo beanInfo = null;
		try {
			beanInfo = Introspector.getBeanInfo(obj.getClass());
		} catch (IntrospectionException e) {
			e.printStackTrace();
		}
		PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
		for (PropertyDescriptor property : propertyDescriptors) {
				String key = property.getName();
					if (key.compareToIgnoreCase("class") == 0) {
						continue; 
					}
					Method getter = property.getReadMethod();
					String value = "";
					try {
						value = getter!=null ? (String)getter.invoke(obj) : null;
					} catch (IllegalAccessException | IllegalArgumentException
							| InvocationTargetException e) {
						e.printStackTrace();
					}
					map.put(key, value);
				}
		
		return map;
	}
	
}
